Option Explicit On

Dim nuevo As Boolean
Public respuesta As String

Private Sub pon_btn_inicio()
    btnNuevo.Enabled = True
    btnEditar.Enabled = True
    btnCancelar.Enabled = False
    btnBuscar.Enabled = True
    btnGrabar.Enabled = False
    btnBorrar.Enabled = True
    btnPrimero.Enabled = True
    btnAnterior.Enabled = True
    btnSiguiente.Enabled = True
    btnUltimo.Enabled = True
    btnSalir.Enabled = True

    tbCodigo.Enabled = False
    tbNombre.Enabled = False
End Sub

Sub pon_btn_normal()
    btnNuevo.Enabled = False
    btnEditar.Enabled = False
    btnCancelar.Enabled = True
    btnBuscar.Enabled = False
    btnGrabar.Enabled = True
    btnBorrar.Enabled = False
    btnPrimero = False
    btnAnterior = False
    btnSiguiente = False
    btnUltimo = False
    btnSalir = False

    tbCodigo.Enabled = True
    tbNombre.Enabled = True
End Sub

Sub pon_btn_anadir()
    btnNuevo.Enabled = False
    btnEditar.Enabled = False
    btnCancelar.Enabled = True
    btnBuscar.Enabled = False
    btnGrabar.Enabled = True
    btnBorrar.Enabled = False
    btnPrimero = False
    btnAnterior = False
    btnSiguiente = False
    btnUltimo = False
    btnSalir = False

    tbCodigo.Enabled = True
    tbNombre.Enabled = True
End Sub

Private Sub btnBuscar_Click()
    frmBuscador.Show()
    If respuesta <> "" Then
        Dim f As Integer
        f = existe(respuesta)
        If f <> -1 Then
            Cells(f, 1).Select()
            mostrar_registro(fNumReg)
            mostrar_posicion()
        End If
    End If
End Sub

Private Sub btnCancelar_Click()
    If fNumReg > fNumRegs Then
        Cells(fNumRegs + 1, 1).Select()
    End If
    mostrar_registro(fNumReg)
    mostrar_posicion()
    pon_btn_inicio()
    nuevo = False
End Sub

Private Sub btnEditar_Click()
    mostrar_registro(fNumReg)
    mostrar_posicion()
    pon_btn_normal()
    nuevo = False
    tbCodigo.SetFocus()
End Sub

Function fNumRegs() As Integer
    Dim nfilas As Integer
    nfilas = Cells.Find("*", SearchOrder:=xlByRows, _
    LookIn:=xlValues, SearchDirection:=xlPrevious).Row
    fNumRegs = nfilas - 1
End Function

Function fNumReg() As Integer
    fNumReg = ActiveCell.Row - 1
End Function

Private Sub btnSalir_Click()
    Unload(Me)
End Sub

Sub mostrar_posicion()
    indicadorSB.Text = fNumReg & " de " & fNumRegs
End Sub

Sub mostrar_registro(nReg As Integer)
    tbCodigo.Text = Cells(nReg + 1, 1).Value
    tbNombre.Text = Cells(nReg + 1, 2).Value
End Sub

Private Sub btnPrimero_Click()
    Range("A2").Select()
    mostrar_registro(fNumReg)
    mostrar_posicion()
End Sub

Private Sub btnSiguiente_Click()
    Dim fila As Integer
    If fNumReg < fNumRegs Then
        fila = fNumReg + 2
    Else
        fila = fNumRegs + 1
    End If
    Cells(fila, 1).Select()
    mostrar_registro(fNumReg)
    mostrar_posicion()
End Sub

Private Sub btnAnterior_Click()
    Dim fila As Integer
    If fNumReg > 1 Then
        fila = fNumReg
    Else
        fila = 2
    End If
    Cells(fila, 1).Select()
    mostrar_registro(fNumReg)
    mostrar_posicion()
End Sub

Private Sub btnUltimo_Click()
    Cells(fNumRegs + 1, 1).Select()
    mostrar_registro(fNumReg)
    mostrar_posicion()
End Sub

Private Sub btnNuevo_Click()
    pon_btn_anadir()
    Cells(fNumRegs + 2, 1).Select()
    tbCodigo.Text = ""
    tbNombre.Text = ""
    tbCodigo.SetFocus()
    nuevo = True
End Sub

Private Sub btnGrabar_Click()
    Dim codigo As String
    codigo = tbCodigo.Text
    If existe(codigo) <> -1 And nuevo Then
        MsgBox("Cdigo (" & codigo & ") ya existe.")
        tbCodigo.SetFocus()
        Exit Sub
    End If
    ActiveCell = tbCodigo.Text
    ActiveCell.Offset(0, 1).Select()
    ActiveCell = tbNombre.Text
    ordenar()
    Cells(fNumReg + 1, 1).Select()
    pon_btn_inicio()
    mostrar_posicion()
End Sub

Private Sub btnBorrar_Click()
    If MsgBox("Est Vd. seguro de la anulacin de (" & tbCodigo.Text & ") ? ", vbYesNo + vbExclamation, "Eliminar registro") = vbYes Then
        Range(Cells(fNumReg + 1, 1), Cells(fNumReg + 1, 2)).Delete(xlShiftUp)
        If fNumReg > fNumRegs Then
            Cells(fNumRegs + 1, 1).Select()
        End If
    End If
    pon_btn_inicio()
    mostrar_registro(fNumReg)
    mostrar_posicion()
End Sub

Private Sub rbCodigo_Click()
    ordenar()
End Sub

Private Sub rbNombre_Click()
    ordenar()
End Sub

Private Sub UserForm_Initialize()
    Range("A2").Select()
    pon_btn_inicio()
    mostrar_registro(fNumReg)
    mostrar_posicion()
    nuevo = False
    rbCodigo.Value = True
    rbNombre = False
End Sub

Sub ordenar()
    Dim r As Range
    r = Range("A1:B11")
    r = Range(Cells(1, 1), Cells(fNumRegs + 1, 2))
    If rbCodigo.Value = True Then
        r.Sort(Key1:="Telfono", order1:=xlAscending, Header:=xlYes)
    Else
        r.Sort(Key1:="Nombre", order1:=xlAscending, Header:=xlYes)
    End If
End Sub

Function existe(codigo As String) As Integer
    Dim s, r As Range
    r = Range(Cells(1, 1), Cells(fNumRegs + 1, 1))
    s = r.Find(What:=codigo, LookIn:=xlValues)
    If Not s Is Nothing Then
        existe = s.Row
    Else
        existe = -1
    End If
End Function

